package com.kkb.web;

import com.kkb.bean.Menu;
import com.kkb.bean.Role;
import com.kkb.service.MenuService;
import com.kkb.service.RoleService;
import com.kkb.service.impl.MenuServiceImpl;
import com.kkb.service.impl.RoleServiceImpl;
import com.kkb.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/power/role/roles"})
public class RoleServlet extends HttpServlet {
    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)) {
            select(req, resp);
        } else if ("selectmenus".equals(method)) {
            selectmenus(req, resp);
        } else if ("insert".equals(method)) {
            insert(req, resp);
        } else if ("update".equals(method)) {
            update(req, resp);
        } else if ("getById".equals(method)) {
            getById(req, resp);
        } else if ("updateById".equals(method)) {
            updateById(req, resp);
        } else if ("delete".equals(method)) {
            delete(req, resp);
        }
    }

    //新增角色
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.insertRole(rolename, state, menuids);
        if (i > 0) {
            resp.sendRedirect("/power/role/roles?method=select");
        } else {
            resp.sendRedirect("/power/role/roles?method=selectmenus");
        }
    }

    //修改角色
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleid = req.getParameter("roleid");
        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = roleService.update(Integer.parseInt(roleid), rolename, state, menuids);
        if (i > 0) {
            resp.sendRedirect("/power/role/roles?method=select");
        }
    }

    //查询菜单列表
    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        //2.调取service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist", menuList);
        req.getRequestDispatcher("add.jsp").forward(req, resp);
    }


    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex = (index == null || index.length() == 0) ? 1 : Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Role> roleList = roleService.getRoleList(pageIndex, pageUtil.getPageSize());
        int total = roleService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(roleList);

        //3.存值跳页面
        req.setAttribute("pi", pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
    }

    // 详情
    protected void getById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("id");
        Role role = roleService.findById(Integer.parseInt(roleId));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist", menuList);
        req.setAttribute("role", role);
        req.getRequestDispatcher("info.jsp").forward(req, resp);
    }

    // 进入修改页面
    protected void updateById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("id");
        Role role = roleService.findById(Integer.parseInt(roleId));
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist", menuList);
        req.setAttribute("role", role);
        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }

    // 删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("id");
        roleService.delete(Integer.parseInt(roleId));
        resp.sendRedirect("/power/role/roles?method=select");
    }
}
